package com.yantu.controller.wx;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yantu.bean.*;
import com.yantu.bean.Class;
import com.yantu.mapper.*;
import com.yantu.util.RedisUtils;
import com.yantu.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.reactive.TransactionalOperatorExtensionsKt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 综合实训课程设计
 * @since 2022-08-13
 */
@Api("微信主页面展示")
@RequestMapping("/wx")
@RestController
public class WxMainPageController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    ClassStudentMapper classStudentMapper;

    @Autowired
    ClassPaperMapper classPaperMapper;

    @Autowired
    ExamPaperAnswerMapper examPaperAnswerMapper;

    @Autowired
    ExamPaperMapper examPaperMapper;

    @Autowired
    ClassMapper classMapper;

    @Autowired
    TaskExamMapper taskExamMapper;

    @Autowired
    TaskExamCustomerAnswerMapper customerAnswerMapper;

    @Autowired
    RedisUtils redisUtils;

    //主页试卷列表
    @GetMapping("/paperList/{token}")
    @ApiOperation("主页试卷列表")
    public Result getPaperList(@PathVariable("token") String token) {

        if(redisUtils.haskeysf("wxPaperList"+ token)){
            return Result.succ(200, "成功", redisUtils.getValue("wxPaperList"+ token));
        }else {
            Map<String, Object> map = new HashMap<>();
            List<Object> list1 = new ArrayList<>();
            List<Object> list2 = new ArrayList<>();
            //找出所有的试卷
            List<ExamPaper> examPapers = examPaperMapper.selectList(null);
            System.out.println("是在这里吗");
            for (ExamPaper examP : examPapers) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("id", examP.getId());
                map1.put("name", examP.getName());
                map1.put("limitStartTime", examP.getLimitStartTime());
                map1.put("limitEndTime", examP.getLimitEndTime());
                if (examP.getPaperType() == 1) {
                    list1.add(map1);
                } else if (examP.getPaperType() == 4) {
                    list2.add(map1);
                }
            }
            map.put("fixedPaper", list1);
            map.put("timeLimitPaper", list2);

            QueryWrapper queryToken = new QueryWrapper<>();
            queryToken.eq("token", token);
            User user = userMapper.selectOne(queryToken);//根据token查询用户信息
            System.out.println("用户信息在这里呀");
            System.out.println(user);
            //查出班级名字
            QueryWrapper query2 = new QueryWrapper<>();
            query2.eq("user_id", user.getId());
            ClassStudent cs = classStudentMapper.selectOne(query2);
            System.out.println("用户班级信息");
            System.out.println(cs);
            if (cs != null) {
                QueryWrapper query3 = new QueryWrapper<>();
                query3.eq("id", cs.getClassId());
                Class cla = classMapper.selectOne(query3);//学生所在班级信息
                System.out.println("用户班级");
                System.out.println(cla);
                QueryWrapper queryyyy = new QueryWrapper<>();
                queryyyy.eq("class_id", cla.getId());
                List<ClassPaper> classPapers = classPaperMapper.selectList(queryyyy);
                System.out.println("班级试卷信息");
                System.out.println(classPapers);
                if (classPapers.size() != 0) {
                    List<Integer> paperNumber = new ArrayList<>();
                    for (ClassPaper cccc : classPapers) {
                        System.out.println("班级试卷Idwei1");
                        System.out.println(cccc.getPaperId());
                        paperNumber.add(cccc.getPaperId());
                    }

                    if (paperNumber != null) {
                        QueryWrapper<ExamPaper> query1 = new QueryWrapper<>();
//                    query1.groupBy("paper_type");
                        query1.in("id", paperNumber);
                        List<ExamPaper> examPapers2 = examPaperMapper.selectList(query1);

                        List<Object> list3 = new ArrayList<>();
                        for (ExamPaper examper : examPapers2) {
                            Map<String, Object> map1 = new HashMap<>();
                            map1.put("id", examper.getId());
                            map1.put("name", examper.getName());

                            map1.put("className", cla.getName());
                            list3.add(map1);

                        }
                        map.put("classPaper", list3);
                        return Result.succ(200, "操作成功", map);
                    } else {
                        map.put("classPaper", null);
                    }
                } else {
                    map.put("classPaper", null);
                }
            } else {
                map.put("classPaper", null);
                System.out.println("yayayyayayayayayay");
            }
            redisUtils.saveValueTime("wxPaperList"+ token,map,1);
            return Result.succ(200, "没有班级试卷信息", map);
        }
    }


//     任务中心

    @GetMapping("/mainTask/{token}")
    @ApiOperation("任务中心")
    public Result getMainTaskList(@PathVariable("token") String token) {
        if (redisUtils.haskeysf("wxMainTask" + token)) {
            List<Object> list = (List<Object>) redisUtils.getValue("wxMainTask" + token);
            return Result.succ(200, "成功", list);
        } else {
            //根据token查询用户信息
            QueryWrapper queryToken = new QueryWrapper<>();
            queryToken.eq("token", token);
            User user = userMapper.selectOne(queryToken);
            if (user != null) {

//        查出班级名字
                QueryWrapper query2 = new QueryWrapper<>();
                query2.eq("user_id", user.getId());
                ClassStudent cs = classStudentMapper.selectOne(query2);
                if (cs != null) {
                    QueryWrapper query3 = new QueryWrapper<>();
                    query3.eq("id", cs.getClassId());
                    Class cla = classMapper.selectOne(query3);
                    //找出所有的test_exam_customer_answer
                    QueryWrapper query4 = new QueryWrapper<>();
                    query4.eq("create_user", cla.getUserId());

                    List<TaskExamCustomerAnswer> taskExamCustomerAnswers = customerAnswerMapper.selectList(query4);
                    List<Object> list = new ArrayList<>();
                    for (TaskExamCustomerAnswer task : taskExamCustomerAnswers) {
                        Map<String, Object> map1 = new HashMap<>();
                        TaskExam taskExam = taskExamMapper.selectById(task.getTaskExamId());
                        map1.put("id", taskExam.getId());
                        map1.put("title", taskExam.getTitle());
                    }

                    List<TaskExam> taskExams = taskExamMapper.selectList(null);

                    for (TaskExam taskExam : taskExams) {
                        Map<String, Object> map = new HashMap<>();

                        map.put("id", taskExam.getId());
                        map.put("title", taskExam.getTitle());
                        //将task表和Exam_paper表对应起来
                        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("task_exam_id", taskExam.getId());
                        ExamPaper examPaper = examPaperMapper.selectOne(queryWrapper);

                        List<Object> list1 = new ArrayList();
                        Map<String, Object> map1 = new HashMap<>();
                        map1.put("examPaperId", examPaper.getId());
                        map1.put("examPaperName", examPaper.getName());

                        //将Exam_paper表和xam_paper_answer对应起来
                        QueryWrapper<ExamPaperAnswer> queryWrapper1 = new QueryWrapper<>();
                        queryWrapper1.eq("exam_paper_id", examPaper.getId());
                        ExamPaperAnswer examPaperAnswer = examPaperAnswerMapper.selectOne(queryWrapper1);
                        if (examPaperAnswer != null) {
                            map1.put("examPaperAnswerId", examPaperAnswer.getId());
                            map1.put("status", examPaperAnswer.getStatus());
                            list1.add(map1);
                            map.put("paperItems", list1);
                        }

                        list.add(map);
                    }
                    System.out.println(list);
                    redisUtils.saveValue("wxMainTask"+ token,list);
                    return Result.succ(200, "查询成功", list);

                } else {
                    return Result.fail("该用户没有班级信息");
                }
            } else {
                return Result.fail("该用户不存在");
            }
        }
    }
}
